import { ILoadMapTypeAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { getContactsPaneQueqyObj, getMemoirPaneQueqyObj, getQualifyPaneQueqyObj } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();

/**
 * 添加供应商W
 * @param callback 接口回调
 * @returns
 */
const addSupplierInfoService = (callback: (res: any, clickType: boolean) => void) => {
  const addSupplierInfo = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierInfoController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    addSupplierInfoRun: addSupplierInfo?.run,
    addSupplierInfoLoading: addSupplierInfo?.loading,
  };
};

/**
 * 编辑供应商
 * @param callback 接口回调
 * @returns
 */
const editSupplierInfoService = (callback: (res: any, clickType: boolean) => void) => {
  const editSupplierInfo = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierInfoController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    editSupplierInfoRun: editSupplierInfo?.run,
    editSupplierInfoLoading: editSupplierInfo?.loading,
  };
};

/**
 * 作废供应商
 * @param callback 接口回调
 * @returns
 */
const cancelSupplierInfoService = (callback: (res: any) => void) => {
  const cancelSupplierInfo = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierInfoController/cancel`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    cancelSupplierInfoRun: cancelSupplierInfo?.run,
    cancelSupplierInfoLoading: cancelSupplierInfo?.loading,
  };
};

/**
 * 拉黑供应商
 * @param callback 接口回调
 * @returns
 */
const pullBlackSupplierInfoService = (callback: (res: any) => void) => {
  const pullBlackSupplierInfo = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierInfoController/pullBlack`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    pullBlackSupplierInfoRun: pullBlackSupplierInfo?.run,
    pullBlackSupplierInfoLoading: pullBlackSupplierInfo?.loading,
  };
};

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getSupplierByIdService = (callback: (res: any) => void) => {
  const getSupplierById = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/supplierInfoController/getSupplierById/${params?.id}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getSupplierByIdRun: getSupplierById?.run,
    getSupplierByIdLoading: getSupplierById?.loading,
    getSupplierByIdInfo: getSupplierById?.data?.obj,
  };
};

/**
 * 添加联系人
 * @param callback 接口回调
 * @returns
 */
const addContactsService = (callback: (res: any) => void) => {
  const addContacts = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierContacterController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    addContactsRun: addContacts?.run,
    addContactsLoading: addContacts?.loading,
  };
};

/**
 * 编辑联系人
 * @param callback 接口回调
 * @returns
 */
const editContactsService = (callback: (res: any) => void) => {
  const editContacts = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierContacterController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    editContactsRun: editContacts?.run,
    editContactsLoading: editContacts?.loading,
  };
};

/**
 * 删除联系人
 * @param callback 接口回调
 * @returns
 */
const deleteContactsService = (callback: (res: any) => void) => {
  const deleteContacts = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/supplierContacterController/delete`;
      return requestFunComponFiex('DELETE', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    deleteContactsRun: deleteContacts?.run,
    deleteContactsLoading: deleteContacts?.loading,
  };
};

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getContacterByIdService = (callback: (res: any) => void) => {
  const getContacterById = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/supplierContacterController/getContacterById/${params?.id}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getContacterByIdRun: getContacterById?.run,
    getContacterByIdLoading: getContacterById?.loading,
    getContacterByIdInfo: getContacterById?.data?.obj,
  };
};

/**
 * 获取资质dataGrid
 * @param curRes 权限
 * @param supplierID 供应商ID
 * @param callback 接口回调
 * @returns
 */
const qualifyDataGridServices = (curRes: Record<string, any>, supplierID: string, callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.QUALIFICA_TYPE}`, //企业资质类型
  ];

  /** 获取权限参数 */
  const queryObj: Record<string, any> = getQualifyPaneQueqyObj(curRes);

  /** 获取列表数据  */
  const { loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        current: 1,
        pageSize: 9999,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        supplierID,
        ...queryObj,
        ...opt,
      };

      const requestURL = `${baseUrl}/supplierQualifyController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 9999,
      onSuccess: callback,
    },
  );

  /**
   * 获取管理也数据
   * @param otherRequest 搜索参数
   */
  const getQualifyPaneDataGrid = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    run({ ...request });
  };

  return {
    getQualifyPaneDataGrid,
    refreshQualifyPane: refresh,
    qualifyPaneloading: loading,
  };
};

/**
 * 添加资质
 * @param callback 接口回调
 * @returns
 */
const addQualifyService = (callback: (res: any) => void) => {
  const addQualify = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierQualifyController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    addQualifyRun: addQualify?.run,
    addQualifyLoading: addQualify?.loading,
  };
};

/**
 * 编辑资质
 * @param callback 接口回调
 * @returns
 */
const editQualifyService = (callback: (res: any) => void) => {
  const editQualify = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierQualifyController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    editQualifyRun: editQualify?.run,
    editQualifyLoading: editQualify?.loading,
  };
};

/**
 * 获取资质详情
 * @param callback  获取详情回调
 * @returns
 */
const getQualifyByIdService = (callback: (res: any) => void) => {
  const getQualifyById = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/supplierQualifyController/getQualifyById/${params?.id}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getQualifyByIdRun: getQualifyById?.run,
    getQualifyByIdLoading: getQualifyById?.loading,
    getQualifyByIdInfo: getQualifyById?.data?.obj,
  };
};

/**
 * 获取负面影响dataGrid
 * @param curRes 权限
 * @param supplierID 供应商ID
 * @param callback 接口回调
 * @returns
 */
const memoirDataGridServices = (curRes: Record<string, any>, supplierID: string, callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.MEMOIR_TYPE}`, //大事件类型
  ];

  /** 获取权限参数 */
  const queryObj: Record<string, any> = getMemoirPaneQueqyObj(curRes);

  /** 获取列表数据  */
  const { loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        current: 1,
        pageSize: 9999,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        supplierID,
        ...queryObj,
        ...opt,
      };

      const requestURL = `${baseUrl}/supplierMemoirController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 9999,
      onSuccess: callback,
    },
  );

  /**
   * 获取管理也数据
   * @param otherRequest 搜索参数
   */
  const getMemoirPaneDataGrid = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    run({ ...request });
  };

  return {
    getMemoirPaneDataGrid,
    refreshMemoirPane: refresh,
    memoirPaneloading: loading,
  };
};

/**
 * 添加负面影响
 * @param callback 接口回调
 * @returns
 */
const addMemoirService = (callback: (res: any) => void) => {
  const addMemoir = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierMemoirController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    addMemoirRun: addMemoir?.run,
    addMemoirLoading: addMemoir?.loading,
  };
};

/**
 * 编辑负面影响
 * @param callback 接口回调
 * @returns
 */
const editMemoirService = (callback: (res: any) => void) => {
  const editMemoir = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/supplierMemoirController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    editMemoirRun: editMemoir?.run,
    editMemoirLoading: editMemoir?.loading,
  };
};

/**
 * 获取负面影响详情
 * @param callback  获取详情回调
 * @returns
 */
const getMemoirByIdService = (callback: (res: any) => void) => {
  const getMemoirById = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/supplierMemoirController/getMemoirById/${params?.id}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getMemoirByIdRun: getMemoirById?.run,
    getMemoirByIdLoading: getMemoirById?.loading,
    getMemoirByIdInfo: getMemoirById?.data?.obj,
  };
};

/**
 * 获取资质dataGrid
 * @param curRes 权限
 * @param supplierID 供应商ID
 * @param callback 接口回调
 * @returns
 */
const contactsDataGridServices = (curRes: Record<string, any>, supplierID: string, callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.NATION_TYPE}`,
    `${getTenantID()}${ILoadMapTypeAll.EDUCATION_LEVEL}`,
    `${getTenantID()}${ILoadMapTypeAll.POLITICAL_OUTLOOK}`,
  ];

  /** 获取权限参数 */
  const queryObj: Record<string, any> = getContactsPaneQueqyObj(curRes);

  /** 获取列表数据  */
  const { loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        current: 1,
        pageSize: 9999,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        supplierID,
        ...queryObj,
        ...opt,
      };

      const requestURL = `${baseUrl}/supplierContacterController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 9999,
      onSuccess: callback,
    },
  );

  /**
   * 获取管理也数据
   * @param otherRequest 搜索参数
   */
  const getContactsPaneDataGrid = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    run({ ...request });
  };

  return {
    getContactsPaneDataGrid,
    refreshContactsPane: refresh,
    contactsPaneloading: loading,
  };
};

export {
  addSupplierInfoService,
  editSupplierInfoService,
  cancelSupplierInfoService,
  pullBlackSupplierInfoService,
  getSupplierByIdService,
  editContactsService,
  addContactsService,
  deleteContactsService,
  getContacterByIdService,
  qualifyDataGridServices,
  addQualifyService,
  editQualifyService,
  getQualifyByIdService,
  memoirDataGridServices,
  addMemoirService,
  editMemoirService,
  getMemoirByIdService,
  contactsDataGridServices,
};
